#!/usr/bin/python
import os
import re
import utility
import collections

class HttpTls:
    
    def missed_tls(self, data):
        dictionary = collections.OrderedDict()
        tlsHostList = re.findall("Pkt: (\d*) \(\d* \w*\), Conn: \d*\(\d*\), App: SSL \(\w*\), State: \w*?, Stack: /ETH/IP/TCP/SSL, Error: \w*\n - tls.host=(.*)", data, flags=0)
        for tlsHost in tlsHostList:
            pktNum = "Pkt: " + tlsHost[0]
            host = "Tls_Host: " + tlsHost[1]
            if not dictionary.has_key(host):
                dictionary[host] = "{: <15}{: <50}\n".format(pktNum, host)
        return dictionary.values()
    
    def missed_http(self, data):
        dictionary = collections.OrderedDict()
        httpHostList = re.findall("Pkt: (\d*) \(\d* \w*\), Conn: \d*\(\d*\), App: HTTP \(\w*\), State: \w*?, Stack: /ETH/IP/TCP/HTTP, Error: \w*\n (- http.request.host=(.+)\n - http.request.referer=(.*)|- http.request.host=(.*))", data, flags=0)
        for httpHost in httpHostList:
            pktNum = "Pkt: " + httpHost[0]
            host = "Host: "
            referer = "Referer: "
            if httpHost[2] != "" and httpHost[3] != "":
                host += httpHost[2]
                referer += httpHost[3]
            else:
                host += httpHost[4]
                referer += "\n"
            if not dictionary.has_key(host):
                dictionary[host] = "{: <15}{: <50}{}\n".format(pktNum, host, referer)
        return dictionary.values()
        
    def missed_http_tls(self, file_path):
        if str(file_path).endswith(".pkt.log"):
            output_path = file_path + ".missed.log"
            lines = []
            with open(output_path, 'w+') as output:
                output.write("\n" + file_path + "\n")
                lines.append("\n" + file_path + "\n")
                with open(file_path, 'r') as f:
                    data = f.read()
                    """ TLS Host """
                    tlsHostList = self.missed_tls(data)
                    output.write("\nTLS:\n")
                    lines.append("\nTLS:\n")
                    for tlsHost in tlsHostList:
                        output.write(tlsHost)
                        lines.append(str(tlsHost))
                    """ HTTP Host """
                    httpHostList = self.missed_http(data)
                    output.write("\nHTTP:\n")
                    lines.append("\nHTTP:\n")
                    for httpHost in httpHostList:
                        output.write(httpHost)
                        lines.append(str(httpHost))
                f.close()    
            output.close()
            if len(lines) <= 3:
                return []
            return lines
        else:
            print "This file is not a flowtool packet log file"

    def missed_tcp(self, file_path):
        if str(file_path).endswith(".flow.log"):
            lines = []
            lines.append("\n" + file_path + "\n")
            lines.append("TCP/UDP:\n")
            output_file = file_path + ".missed.log"
            input_file = open(file_path, 'r')
            dictionary = collections.OrderedDict()
            tcpList = re.findall('[ \t]+Connection ID\: \d+\n\s+Started on Packet\: \d+\n\s+Client Details: .+\n\s+Server Details: .+\n\s+Classification: TCP \([A-Z]+\)\(.+\)\n\s+Connection State: .*\n|\s+Connection ID\: \d+\n\s+Started on Packet\: \d+\n\s+Client Details: .+\n\s+Server Details: .+\n\s+Classification: UDP \([A-Z]+\)\(.+\)\n\s+Connection State: .*\n', input_file.read(), flags=0)
            for tcp in tcpList:
                if not dictionary.has_key(tcp):
                    dictionary[tcp] = tcp
                    lines.append(tcp)
                    lines.append("\n")
            with open(output_file, 'w+') as output:
                for tcp in lines:
                    output.write(str(tcp))
            input_file.close()
            output.close()
            if len(lines) <= 3: 
                return []
            return lines
        else:
            print "This file is not a flowtool flow log file"
                
    def missed_http_tls_tcp_folder(self, directory, output_path):
        if os.path.isdir(directory):
            pkt_files = utility.Directory(directory, ".pkt.log")
            flow_files = utility.Directory(directory, ".flow.log")
            with open(output_path, 'w+') as output:
                print "Total number of file: %s" % (str(pkt_files.count()))
                for f in pkt_files:
                    datalist = self.missed_http_tls(f)
                    print "missed_http_tls: " + f + "\t" + pkt_files.percentage(f)
                    if len(datalist) > 0:
                        for item in datalist:
                            output.write(str(item))
                print "Total number of file: %s" % (str(flow_files.count()))
                for f in flow_files:
                    datalist = self.missed_tcp(f)
                    print "missed_tcp: " + f + "\t" + flow_files.percentage(f)
                    if len(datalist) > 0:
                        for item in datalist:
                            output.write(str(item))
                    
            output.close()
        else:
            print "Not a valid directory %s" % directory
    
    
